package org.eoti.kama.settings;

import com.ardor3d.framework.DisplaySettings;
import org.eoti.util.plugin.SettingsPlugin;

import java.awt.*;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class DisplaySettingsPlugin
extends SettingsPlugin
{
	public DisplaySettingsPlugin()
	{
		super( "display", "org/eoti/kama/settings/DisplaySettingsPlugin");
	}

	enum PropertyName
	{
		width,
		height,
		colorDepth,
		frequency,
		alphaBits,
		bitDepth,
		stencilBits,
		samples,
		fullscreen,
		stereo
	}
	
	private String getProperty(PropertyName key)
	{
		return super.getProperty( key.name() );
	}

	private Integer getIntProperty(PropertyName key)
	{
		return super.getIntProperty(key.name());
	}

	private Boolean getBoolProperty(PropertyName key)
	{
		return super.getBoolProperty(key.name());
	}

	private void setProperty(PropertyName key, String value)
	{
		super.setProperty( key.name(), value );
	}

	private void setProperty(PropertyName key, Integer value)
	{
		super.setProperty( key.name(), value.toString() );
	}

	private void setProperty(PropertyName key, Boolean value)
	{
		super.setProperty( key.name(), value.toString() );
	}

	@Override public void initialize()
	{
		super.initialize();

		GraphicsEnvironment _gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice _gfxDevice = _gfxEnv.getDefaultScreenDevice();
		GraphicsConfiguration _gfxConfig = _gfxDevice.getDefaultConfiguration();
		DisplayMode _gfxMode = _gfxDevice.getDisplayMode();

		setProperty( PropertyName.width, _gfxMode.getWidth());
		setProperty( PropertyName.height, _gfxMode.getHeight());
		setProperty( PropertyName.colorDepth, _gfxConfig.getColorModel().getPixelSize());
		setProperty( PropertyName.frequency, 0);
		setProperty( PropertyName.alphaBits, 0);
		setProperty( PropertyName.bitDepth, _gfxMode.getBitDepth());
		setProperty( PropertyName.stencilBits, 0);
		setProperty( PropertyName.samples, 0);
		setProperty( PropertyName.fullscreen, true);
		setProperty( PropertyName.stereo, false);
	}

	public int getWidth(){return getIntProperty( PropertyName.width);}
	public int getHeight(){return getIntProperty( PropertyName.height);}
	public int getColorDepth(){return getIntProperty( PropertyName.colorDepth);}
	public int getFrequency(){return getIntProperty( PropertyName.frequency);}
	public int getAlphaBits(){return getIntProperty( PropertyName.alphaBits);}
	public int getBitDepth(){return getIntProperty( PropertyName.bitDepth);}
	public int getStencilBits(){return getIntProperty( PropertyName.stencilBits);}
	public int getSamples(){return getIntProperty( PropertyName.samples);}
	public boolean isFullScreen(){return getBoolProperty( PropertyName.fullscreen);}
	public boolean isStereo(){return getBoolProperty( PropertyName.stereo);}
	public DisplaySettings getCurrentDisplaySettings()
	{
		return new DisplaySettings(
				getWidth(),
		    getHeight(),
		    getColorDepth(),
		    getFrequency(),
		    getAlphaBits(),
		    getBitDepth(),
		    getStencilBits(),
		    getSamples(),
		    isFullScreen(),
		    isStereo()
		);
	}
}
